{% extends theme_path('base.html') %}

{% block title %}CUSTOM-TEMPLATE-{{ current_category.name }}{% endblock %}

{% block meta %}
    {% if current_category %}
        <meta name="keywords" content="{{ current_category.name }},{{ site_config.get_config('site_keywords', 'PPress,技术,博客') }}">
        <meta name="description" content="浏览{{ current_category.name }}分类下的所有文章 - {{ current_category.description }}">
    {% else %}
        {{ super() }}
    {% endif %}
{% endblock %}

{% block content %}
    <div class="grid grid-cols-1 lg:grid-cols-12 gap-6 max-w-screen-xl mx-auto px-4">
        <!-- 文章列表 -->
        <div class="lg:col-span-8">
            <div class="space-y-6">
                {% for article in articles.items %}
                    <article class="bg-white dark:bg-gray-800 rounded-lg shadow-sm overflow-hidden transition-colors duration-200">
                        <div class="p-4 sm:p-6">
                            <h2 class="text-xl font-bold mb-2">
                                <a href="{{ ArticleUrlGenerator.generate(article.id, article.category_id, article.created_at) }}"
                                   class="text-gray-900 dark:text-white hover:text-blue-600 dark:hover:text-blue-400">
                                    {{ article.title }}
                                </a>
                            </h2>
                            <div class="flex items-center text-sm text-gray-500 dark:text-gray-400 mb-4">
                                {% set author = get_author_info(article) %}
                                <div class="flex items-center flex-shrink-0">
                                    <img src="{{ author.avatar }}" alt="avatar" class="w-8 h-8 rounded-full">
                                    {% if author.id %}
                                        <a href="{{ url_for('user.author', id=author.id) }}"
                                           class="ml-2 text-sm font-medium dark:text-white text-gray-900 hover:text-blue-600">
                                            {{ author.nickname }}
                                        </a>
                                    {% else %}
                                        <span class="ml-2 text-sm text-gray-500">{{ author.nickname }}</span>
                                    {% endif %}
                                </div>
                                <div class="hidden sm:flex items-center">
                                    <span class="mx-2">•</span>
                                    <span>{{ article.created_at.strftime('%Y-%m-%d %H:%M') }}</span>
                                    <span class="mx-2">•</span>
                                    <span>{{ article.view_count }} 阅读</span>
                                </div>
                            </div>
                            <div class="text-gray-600 dark:text-gray-300 mb-4 line-clamp-3">
                                {{ article.content|striptags|truncate(200) }}
                            </div>

                            <!-- 遍历所有自定义字段 -->
                            {% if article.fields %}
                                <div class="mt-4 space-y-2">
                                    {% for key, value in article.fields.items() %}
                                        <div class="flex items-center text-sm">
                                            <span class="text-gray-500 dark:text-gray-400">{{ key }}:</span>
                                            <span class="ml-2">{{ value }}</span>
                                        </div>
                                    {% endfor %}
                                </div>
                            {% endif %}

                            <!-- 副标题 -->
                            {% if article.fields and article.fields.subtitle %}
                                <h2 class="text-xl text-gray-600 dark:text-gray-400 mt-2">
                                    {{ article.fields.subtitle }}
                                </h2>
                            {% endif %}

                            <!-- 缩略图 -->
                            {% if article.fields and article.fields.thumbnail %}
                                <div class="mt-4">
                                    <img src="{{ article.fields.thumbnail }}"
                                         alt="{{ article.title }}"
                                         class="w-full rounded-lg">
                                </div>
                            {% endif %}

                            <!-- 使用 get_field 方法,可以指定默认值 -->
                            {{ article.get_field('thumbnail', '/static/images/default-thumbnail.jpg') }}


                            <!-- 根据主题配置显示字段 -->
                            {% for field_key, field in theme.article_fields.items() %}
                                {% if article.fields and field_key in article.fields %}
                                    {% if field.type == 'image' %}
                                        <img src="{{ article.fields[field_key] }}"
                                             alt="{{ field.name }}"
                                             class="w-full rounded-lg">
                                    {% else %}
                                        <div class="field-{{ field_key }}">
                                            <span class="field-name">{{ field.name }}:</span>
                                            <span class="field-value">{{ article.fields[field_key] }}</span>
                                        </div>
                                    {% endif %}
                                {% endif %}
                            {% endfor %}

                            <div class="flex flex-wrap items-center gap-2 sm:gap-4">
                                <a href="{{ ArticleUrlGenerator.generate(article.id, article.category_id, article.created_at) }}"
                                   class="text-blue-600 dark:text-blue-400 hover:text-blue-700 dark:hover:text-blue-300">
                                    阅读全文
                                </a>
                                {% if article.tags %}
                                    <div class="flex flex-wrap items-center gap-2">
                                        {% for tag in article.tags %}
                                            <a href="{{ url_for('blog.tag', tag_id_or_slug=tag.id if not tag.use_slug else tag.slug) }}"
                                               class="text-sm text-gray-600 dark:text-gray-400 hover:text-blue-600 dark:hover:text-blue-400">
                                                #{{ tag.name }}
                                            </a>
                                        {% endfor %}
                                    </div>
                                {% endif %}
                            </div>
                        </div>
                    </article>
                {% else %}
                    <div class="text-center py-12 bg-white dark:bg-gray-800 rounded-lg shadow-sm">
                        <p class="text-gray-500 dark:text-gray-400">暂无文章</p>
                    </div>
                {% endfor %}
            </div>

            <!-- 分页 -->
            {% if articles.pages > 1 %}
                <div class="mt-6 bg-white dark:bg-gray-800 rounded-lg shadow-sm p-4">
                    {% with endpoint=endpoint|default('blog.index'),
                    kwargs=({
                        'id': current_category.slug if current_category and current_category.use_slug else current_category.id
                    } if current_category else {}) %}
                        {% include theme_path('components/pagination.html') %}
                    {% endwith %}
                </div>
            {% endif %}
        </div>

        <!-- 侧边栏 -->
        {% block sidebar %}
            <div class="lg:col-span-4 space-y-6">
                <!-- 今日热门 -->
                {% block hot_today %}
                    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm p-6">
                        <h2 class="text-lg font-bold mb-4 text-gray-900 dark:text-white">今日热门</h2>
                        <div class="space-y-3">
                            {% for article, views in hot_articles_today %}
                                <div class="flex items-center space-x-2">
                                    <svg class="w-4 h-4 text-blue-500 animate-pulse" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                              d="M17.657 18.657A8 8 0 016.343 7.343S7 9 9 10c0-2 .5-5 2.986-7C14 5 16.09 5.777 17.656 7.343A7.975 7.975 0 0120 13a7.975 7.975 0 01-2.343 5.657z" />
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                              d="M9.879 16.121A3 3 0 1012.015 11L11 14H9c0 .768.293 1.536.879 2.121z" />
                                    </svg>
                                    <a href="{{ ArticleUrlGenerator.generate(article.id, article.category_id, article.created_at) }}"
                                       class="flex-1 text-sm text-gray-700 dark:text-gray-300 hover:text-blue-600 dark:hover:text-blue-400 truncate">
                                        {{ article.title }}
                                    </a>
                                    <span class="text-xs text-gray-500">{{ views }} 阅读</span>
                                </div>
                            {% else %}
                                <p class="text-gray-500 dark:text-gray-400 text-center py-4">暂无数据</p>
                            {% endfor %}
                        </div>
                    </div>
                {% endblock %}

                <!-- 本周热门 -->
                {% block hot_week %}
                    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm p-6">
                        <h2 class="text-lg font-bold mb-4 text-gray-900 dark:text-white">本周热门</h2>
                        <div class="space-y-3">
                            {% for article, views in hot_articles_week %}
                                <div class="flex items-center group hover:bg-gray-50 dark:hover:bg-gray-700/50 -mx-2 px-2 py-1.5 rounded-lg transition-colors duration-200">
                        <span class="w-5 h-5 text-blue-500/75 dark:text-blue-400/75 font-medium text-xs flex items-center justify-center">
                            {{ loop.index }}
                        </span>
                                    <a href="{{ ArticleUrlGenerator.generate(article.id, article.category_id, article.created_at) }}"
                                       class="flex-1 text-sm text-gray-700 dark:text-gray-300 group-hover:text-blue-600 dark:group-hover:text-blue-400 truncate ml-2">
                                        {{ article.title }}
                                    </a>
                                    <span class="text-xs text-gray-400 dark:text-gray-500 ml-2">{{ views }} 阅读</span>
                                </div>
                            {% else %}
                                <p class="text-gray-500 dark:text-gray-400 text-center py-4">暂无数据</p>
                            {% endfor %}
                        </div>
                    </div>
                {% endblock %}

                <!-- 随机推荐 -->
                {% block random_articles %}
                    <div class="hidden lg:block bg-white dark:bg-gray-800 rounded-lg shadow-sm p-6">
                        <h2 class="text-lg font-bold mb-4 text-gray-900 dark:text-white">随机推荐</h2>
                        <div class="space-y-3">
                            {% for article in random_articles %}
                                <div class="group flex items-center space-x-3 hover:bg-gray-50 dark:hover:bg-gray-700/50 -mx-2 px-2 py-1.5 rounded-lg transition-colors duration-200">
                                    <svg class="w-4 h-4 text-blue-400/75 dark:text-blue-300/75 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                              d="M11.049 2.927c.3-.921 1.603-.921 1.902 0l1.519 4.674a1 1 0 00.95.69h4.915c.969 0 1.371 1.24.588 1.81l-3.976 2.888a1 1 0 00-.363 1.118l1.518 4.674c.3.922-.755 1.688-1.538 1.118l-3.976-2.888a1 1 0 00-1.176 0l-3.976 2.888c-.783.57-1.838-.197-1.538-1.118l1.518-4.674a1 1 0 00-.363-1.118l-3.976-2.888c-.784-.57-.38-1.81.588-1.81h4.914a1 1 0 00.951-.69l1.519-4.674z" />
                                    </svg>
                                    <a href="{{ ArticleUrlGenerator.generate(article.id, article.category_id, article.created_at) }}"
                                       class="flex-1 text-sm text-gray-700 dark:text-gray-300 group-hover:text-blue-600 dark:group-hover:text-blue-400 line-clamp-2">
                                        {{ article.title }}
                                    </a>
                                    <span class="text-xs text-gray-400 dark:text-gray-500">{{ article.view_count }} 阅读</span>
                                </div>
                            {% else %}
                                <p class="text-gray-500 dark:text-gray-400 text-center py-4">暂无文章</p>
                            {% endfor %}
                        </div>
                    </div>
                {% endblock %}

                <!-- 标签云 -->
                {% block random_tags %}
                    <div class="hidden lg:block bg-white dark:bg-gray-800 rounded-lg shadow-sm p-6">
                        <h2 class="text-lg font-bold mb-4 text-gray-900 dark:text-white">标签云</h2>
                        <div class="flex flex-wrap gap-2">
                            {% for tag in random_tags %}
                                <a href="{{ url_for('blog.tag', tag_id_or_slug=tag.id if not tag.use_slug else tag.slug) }}"
                                   class="px-3 py-1 text-sm bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300 rounded-full hover:bg-blue-100 dark:hover:bg-blue-900 hover:text-blue-600 dark:hover:text-blue-400 transition-colors duration-200">
                                    {{ tag.name }}
                                </a>
                            {% else %}
                                <p class="text-gray-500 dark:text-gray-400 text-center w-full">暂无标签</p>
                            {% endfor %}
                        </div>
                    </div>
                {% endblock %}
                <!-- 最新评论 -->
                {% block latest_comments %}
                    <div class="hidden lg:block bg-white dark:bg-gray-800 rounded-lg shadow-sm p-6">
                        <h2 class="text-lg font-bold mb-4 text-gray-900 dark:text-white">最新评论</h2>
                        <div class="space-y-4">
                            {% for comment, user, article in latest_comments %}
                                <div class="border-b dark:border-gray-700 last:border-0 pb-4 last:pb-0">
                                    <!-- 用户信息行 -->
                                    <div class="flex items-center mb-2">
                                        <img src="{{ user.avatar }}" alt="avatar" class="w-6 h-6 rounded-full">
                                        {% if user.id %}
                                            <a href="{{ url_for('user.author', id=user.id) }}"
                                               class="ml-2 text-sm font-medium text-gray-900 dark:text-white hover:text-blue-600 dark:hover:text-blue-400">
                                                {{ user.nickname }}
                                            </a>
                                        {% else %}
                                            <span class="ml-2 text-sm font-medium text-gray-500">{{ user.nickname }}</span>
                                        {% endif %}
                                        <span class="mx-2 text-xs text-gray-400">•</span>
                                        <span class="text-xs text-gray-400">{{ comment.created_at.strftime('%Y-%m-%d') }}</span>
                                    </div>

                                    <!-- 评论内容 -->
                                    <div class="text-sm text-gray-600 dark:text-gray-400 line-clamp-2 mb-2">
                                        {{ comment.content }}
                                    </div>

                                    <!-- 文章链接 -->
                                    <a href="{{ ArticleUrlGenerator.generate(article.id, article.category_id, article.created_at) }}"
                                       class="text-xs text-gray-500 dark:text-gray-400 hover:text-blue-600 dark:hover:text-blue-400 line-clamp-1">
                                        评论于：{{ article.title }}
                                    </a>
                                </div>
                            {% else %}
                                <p class="text-gray-500 dark:text-gray-400 text-center">暂无评论</p>
                            {% endfor %}
                        </div>
                    </div>
                {% endblock %}
            </div>
        {% endblock %}
    </div>
{% endblock %}